<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Enums\OpenShop\InvestmentPromotion\UserContractEnum;
use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\InvestmentPromotion\UserContractValidate;
use App\Http\Validate\OpenShop\Renew\RenewValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use App\Service\OpenShop\Feishu\UserExtContractService;
use App\Service\OpenShop\UserContractService;
use App\Service\Renew\RenewOrderService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class UserContractController extends BaseController
{

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function templateList(Request $request): JsonResponse
    {
        $mode = (int)$request->get('mode', OpenShopContractTemplateModel::MODE_1); // 加盟模式 1-传统模式 2分账模式, 3定金模式
        $contractTypeV2 = (int)$request->get('contractType', UserContractEnum::CONTRACT_TYPE_1);
        return $this->success(UserContractService::templateList($mode,$contractTypeV2));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function insert(Request $request): JsonResponse
    {
        $params = UserContractValidate::insertValidate($request->all());
        return $this->success(UserContractService::insert(getRmsUserInfoByAuth(), $params));
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function update(Request $request, string $orderNo): JsonResponse
    {
        $params = UserContractValidate::updateValidate($request->merge(['orderNo' => $orderNo])->all());
        UserContractService::update(getRmsUserInfoByAuth()->id, $params, $orderNo);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     */
    public function detail(Request $request, string $orderNo): JsonResponse
    {
        UserContractValidate::detailValidate($request->merge(['orderNo' => $orderNo])->all());
        return $this->success(UserContractService::detail($orderNo));
    }

    /**
     * 处理契约锁合同状态回调
     * @param Request $request
     */
    public function contractSigningNotify(Request $request)
    {
        $data = $request->all();
        Log::info('签约合同回调信息:' . json_encode($data, JSON_UNESCAPED_UNICODE));
        UserExtContractService::ContractSigningNotify($data);
    }

    /**
     * 处理契约锁合同状态回调
     * @param Request $request
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     */
    public function contractSigningNotifyV2(Request $request)
    {
        $data = $request->all();

        $mode        = (int)$request->get('mode', 1);              //加盟模式 1-传统模式 2分账模式
        $contractors = (int)$request->get('contractors', 1);       //签约主体 1-个人 2-公司
        Log::info('签约合同回调信息:' . json_encode($data, JSON_UNESCAPED_UNICODE));
        UserExtContractService::ContractSigningNotify($data, 2, $contractors, $mode);
    }


    /**
     * @param String $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function handleExceptionMsg(string $orderNo): JsonResponse
    {
        UserContractService::handleExceptionMsg(getRmsUserInfoByAuth(), $orderNo);
        return $this->success();
    }

    /**
     * @param String $orderNo
     * @return JsonResponse
     * @throws Exception
     */
    public function generateUserContract(string $orderNo): JsonResponse
    {
        UserContractService::generateUserContract($orderNo);
        return $this->success();
    }


    /**
     * 作废合同
     *
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function invalidContract(Request $request, string $orderNo): JsonResponse
    {
        UserContractValidate::invalidContractValidate($request->merge(["orderNo" => $orderNo])->all());
        UserContractService::invalidContract($orderNo);
        return $this->success();
    }

    /**
     * 处理契约锁合同状态回调
     * @param Request $request
     * @return string
     * @throws ContainerExceptionInterface
     * @throws GuzzleException
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function commonSignNotify(Request $request): string
    {

        $data = $request->all();
        Log::info('签约合同回调信息:' . json_encode($data, JSON_UNESCAPED_UNICODE));
        UserExtContractService::commonSignNotify($data);
        return "success";
    }

    /**
     * 合同催签
     *
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function contractNotice(): JsonResponse
    {
        UserContractService::contractNotice(RenewValidate::contractNoticeValidate());
        return $this->success();
    }
}
